package com.cloudcross.ssp.service.impl;

import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.model.DeliverAnalysis;
import com.cloudcross.ssp.service.IAdReportService;
import com.cloudcross.ssp.service.IAdvBalanceService;
import com.cloudcross.ssp.web.multiplatform.AdGroupBasicData;
import com.cloudcross.ssp.web.multiplatform.AdGroupOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class AdReportService implements IAdReportService {
    @Autowired
    private GenericIBatisDao iBatisDao;
    @Autowired
    private IAdvBalanceService advBalanceService;

    @Override
    public DeliverAnalysis findById(Long id) {
        return null;
    }

    @Override
    public int countByParams(Map<String, Object> paramMap) {
        return 0;
    }

    @Override
    public List<DeliverAnalysis> findByParams(Map<String, Object> paramMap, Pager pager) {
        return null;
    }

    @Override
    public boolean add(DeliverAnalysis t) {
        return false;
    }

    @Override
    public boolean edit(DeliverAnalysis t) {
        return false;
    }

    @Override
    public boolean updateStatus(List<Long> idList, int status) {
        return false;
    }

    @Override
    public Map<String, Object> findCompare(Map<String, Object> paramMap) {
        Long advertiserId = null;
        Long agentId = null;

        /**
         * 判断下是广告主，还是代理商
         */
        if (!paramMap.containsKey("advertiserId")) {
            advertiserId = null;
        } else {
            if (paramMap.get("advertiserId") != null && !"".equals(paramMap.get("advertiserId"))) {
                advertiserId = Long.parseLong((String) paramMap.get("advertiserId"));
            } else {
                advertiserId = null;
            }
        }

        if (!paramMap.containsKey("agentId")) {
            agentId = null;
        } else {
            if (paramMap.get("agentId") != null && !"".equals(paramMap.get("agentId"))) {
                agentId = Long.parseLong(String.valueOf(paramMap.get("agentId")));
            } else {
                agentId = null;
            }
        }

        // 查询用户当前余额
        // 格式化
        DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.CHINESE);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        df.applyPattern("0.00");
        Map<String, Object> compareMap = new HashMap<String, Object>();
        /**
         * 先判断传递进来的是一个广告主id，还是代理商id 如果广告主id不为空，则为广告主账号，如果是代理商id不为空则为代理商账号
         */
        if (null != advertiserId) {
            Map<String, Object> map = null;
            paramMap.put("advertiserId", advertiserId);
            // Advertiser advertiser = advertiserService.findById(advertiserId);
            Double balance = advBalanceService.findBalanceByAdvertiserId(advertiserId);

            if (balance == null) {
                balance = 0.0;
            }
            /**
             * 下面是查找该广告主下的昨天和前天的曝光数和点击数
             */
            paramMap.put("date", sdf.format(new Date().getTime() - 24 * 60 * 60 * 1000));
            Long yesterdayImpressionSum = null;
            Long yesterdayClickSum = null;

            map = findTotalImpressionAndClickByAdvertiserId(paramMap);

            /**
             * 对于查询的结果进行判断，如果没找到的结果是空的，则两个值都设为0 如果找到其中的一个值为空就把这个值设为0
             */
            if (null == map) {
                yesterdayClickSum = new Long(0);
                yesterdayImpressionSum = new Long(0);
            } else {
                yesterdayClickSum = Long.parseLong(map.get("totalClick").toString());
                yesterdayImpressionSum = Long.parseLong(map.get("totalImpression").toString());
            }

            compareMap.put("totalImpression", yesterdayImpressionSum);
            compareMap.put("totalClick", yesterdayClickSum);

            paramMap.put("date", sdf.format(new Date().getTime() - 2 * 24 * 60 * 60 * 1000));
            Long beforeYesterdayImpressionSum = null;
            Long beforeYesterdayClickSum = null;
            map = findTotalImpressionAndClickByAdvertiserId(paramMap);

            /**
             * 对于查询的结果进行判断，如果m找到的结果是空的，则两个值都设为0 如果找到其中的一个值为空就把这个值设为0
             */
            if (null == map) {
                beforeYesterdayClickSum = new Long(0);
                beforeYesterdayImpressionSum = new Long(0);
            } else {
                beforeYesterdayClickSum = Long.parseLong(map.get("totalClick").toString());
                beforeYesterdayImpressionSum = Long.parseLong(map.get("totalImpression").toString());
            }

            /**
             * 通过比对昨天和前天的数据大小判断箭头
             */
            if ((yesterdayImpressionSum - beforeYesterdayImpressionSum) > 0) {
                compareMap.put("impressionStatus", "up");
            } else if ((yesterdayImpressionSum == beforeYesterdayImpressionSum)) {
                compareMap.put("impressionStatus", "equal");
            } else {
                compareMap.put("impressionStatus", "down");
            }

            if ((yesterdayClickSum - beforeYesterdayClickSum) > 0) {
                compareMap.put("clickStatus", "up");
            } else if ((yesterdayClickSum == beforeYesterdayClickSum)) {
                compareMap.put("clickStatus", "equal");
            } else {
                compareMap.put("clickStatus", "down");
            }

            compareMap.put("balance", df.format(balance));
        }

        if (null != agentId) {
            paramMap.put("agentId", agentId);

            // 得到代理商账号的余额
            Double balance = advBalanceService.findBalanceByAgentId(agentId);

            // 如果找到的余额为空则余额为0
            if (null == balance) {
                balance = new Double(0);
            }

            Map<String, Object> map = null;
            /**
             * 下面是查找该代理商下的所有广告主的昨天和前天的曝光数和点击数
             */
            paramMap.put("date", sdf.format(new Date().getTime() - 24 * 60 * 60 * 1000));

            Long yesterdayImpressionSum = null;
            Long yesterdayClickSum = null;
            map = findTotalImpressionAndClickByAgentId(paramMap);

            /**
             * 对于查询的结果进行判断，如果没找到的结果是空的，则两个值都设为0 如果找到其中的一个值为空就把这个值设为0
             */
            if (null == map) {
                yesterdayClickSum = new Long(0);
                yesterdayImpressionSum = new Long(0);
            } else {
                yesterdayClickSum = Long.parseLong(map.get("totalClick").toString());
                yesterdayImpressionSum = Long.parseLong(map.get("totalImpression").toString());
            }

            compareMap.put("totalImpression", yesterdayImpressionSum);
            compareMap.put("totalClick", yesterdayClickSum);

            paramMap.put("date", sdf.format(new Date().getTime() - 2 * 24 * 60 * 60 * 1000));
            Long beforeYesterdayImpressionSum = null;
            Long beforeYesterdayClickSum = null;
            map = findTotalImpressionAndClickByAgentId(paramMap);

            /**
             * 对于查询的结果进行判断，如果m找到的结果是空的，则两个值都设为0 如果找到其中的一个值为空就把这个值设为0
             */
            if (null == map) {
                beforeYesterdayClickSum = new Long(0);
                beforeYesterdayImpressionSum = new Long(0);
            } else {
                beforeYesterdayClickSum = Long.parseLong(map.get("totalClick").toString());
                beforeYesterdayImpressionSum = Long.parseLong(map.get("totalImpression").toString());
            }

            /**
             * 通过比对昨天和前天的数据大小判断箭头
             */
            if ((yesterdayImpressionSum - beforeYesterdayImpressionSum) > 0) {
                compareMap.put("impressionStatus", "up");
            } else if ((yesterdayImpressionSum == beforeYesterdayImpressionSum)) {
                compareMap.put("impressionStatus", "equal");
            } else {
                compareMap.put("impressionStatus", "down");
            }

            if ((yesterdayClickSum - beforeYesterdayClickSum) > 0) {
                compareMap.put("clickStatus", "up");
            } else if ((yesterdayClickSum == beforeYesterdayClickSum)) {
                compareMap.put("clickStatus", "equal");
            } else {
                compareMap.put("clickStatus", "down");
            }

            compareMap.put("balance", df.format(balance));
        }

        return compareMap;
    }

    @Override
    public HashMap<String, Object> findTotalImpressionAndClickByAdvertiserId(Map<String, Object> paramMap) {
        return iBatisDao.get("adReportSqlMapper.findTotalImpressionAndClickByAdvertiserId", paramMap);
    }

    @Override
    public HashMap<String, Object> findTotalImpressionAndClickByAgentId(Map<String, Object> paramMap) {
        return iBatisDao.get("adReportSqlMapper.findTotalImpressionAndClickByAgentId", paramMap);
    }

    @Override
    public List<AdGroupBasicData> queryAppBasicData(String yesterday) {
        return iBatisDao.getList("rptDetailSqlMapper.queryAppBasicData", yesterday);
    }

    @Override
    public List<AdGroupOrderVo> queryGroupOrderParam() {
        return iBatisDao.getList("rptDetailSqlMapper.queryGroupOrderParam", null);
    }

    @Override
    public List<AdGroupBasicData> queryPcBasicData(String yesterday) {
        return iBatisDao.getList("rptDetailSqlMapper.queryPcBasicData", yesterday);
    }
}
